<%@page import="com.zhuoshi.bean.ManagerRoleValueBean"%>
<%@page import="com.zhuoshi.dao.ManagerRoleValueDao"%>
<%@page import="com.zhuoshi.bean.NavigationBean"%>
<%@page import="com.zhuoshi.dao.NavigationDao"%>
<%@page import="com.zhuoshi.bean.ManagerRoleBean"%>
<%@page import="com.zhuoshi.dao.ManagerRoleDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

ManagerRoleDao dao = new ManagerRoleDao();
List<ManagerRoleBean> list = dao.getList();
pageContext.setAttribute("list", list);

		NavigationDao treeDao = new NavigationDao();
   		List<NavigationBean> nodes = treeDao.getDataList(0);
   		pageContext.setAttribute("nodes", nodes);
   		
   		
   		String action = request.getParameter("action");
   		//更新权限
   		if("update".equals(action)){
   			String[] nav_id = request.getParameterValues("nav_id");
   			String role = request.getParameter("role");
   			
   			request.setAttribute("role", role);
   			
   			ManagerRoleValueDao mrvDao = new ManagerRoleValueDao();
   			//先执行一次删除操作，把里面的所有相关的角色权限都删除，然后再重新写入
   			int delRows = mrvDao.delete(Integer.parseInt(role));
   			out.print("["+role+"]");
   			int rows = 0;
   			for(String str : nav_id){
   			//out.print(str + " ");
   			ManagerRoleValueBean bean = new ManagerRoleValueBean();
   			bean.setRole_id(Integer.parseInt(role));
   			bean.setNav_id(Integer.parseInt(str));
   			bean.setAction_type("SHOW");
   			rows = rows + mrvDao.add(bean);
   			}
   			if(rows>0){
   				out.print("<script language='javascript'>alert('权限更新成功');location.href='rolemanager.jsp';</script>");
   			}else{
   				out.print("<script language='javascript'>alert('权限更新失败');</script>");
   			}
   			request.removeAttribute("role");
   			
   		}
   		//获取权限
   		if("getAuth".equals(action)){
   			//out.print("获取权限");
   			String role = request.getParameter("role");
   			request.setAttribute("role", role);
   			ManagerRoleValueDao rvDao = new ManagerRoleValueDao();
   			List<ManagerRoleValueBean> rvList = rvDao.getList(Integer.parseInt(role));
   			request.setAttribute("rvList", rvList);
   		}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>角色权限分配</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style>
	body{font-size:16px;}
	</style>
  </head>
  <script>
  var xhr;
  function createXMLHttp(){
  	if(window.ActiveXObject){
  		xhr = new ActiveXObject("Microsoft.XMLHTTP");
  	}else{
  		xhr = new XMLHttpRequest();
  	}
  }
  function checkAuthority(){
  	
  }
  
  </script>
  <body>
  	<form action="?action=getAuth" method="post">
    <select name="role" id="role">
    <!-- <option value="">---请选择角色---</option> -->
    <c:forEach items="${list }" var="bean">
    <c:if test="${bean.role_type>1 }">
    <option value="${bean.id }" ${requestScope.role==bean.id? "selected" : "" }>${bean.role_name }</option>
    </c:if>
    </c:forEach>
    </select>
    
    <input type="submit" value="  获取角色权限  " />
    <hr />
    
    </form>
    
    
    
    <c:if test="${requestScope.role !=null }">
    
    <form action="?action=update" method="post">
    <input type="submit" value="更新权限" />
    <input type="hidden" name="role" value="${requestScope.role }"/>
    <div style="margin:5px 5px 5px 50px">
    <c:forEach items="${nodes }" var="node">
    <c:if test="${node.class_layer==1 }">
    <c:set var="flag" value="false"></c:set>
    <c:forEach items="${rvList }" var="rvBean">
   <c:if test="${node.id==rvBean.nav_id }">
   <c:set var="flag" value="true"></c:set>
   </c:if> 
    </c:forEach>
    <c:if test="${flag==true }">
    ${node.title } <input type="checkbox" name="nav_id" value="${node.id }" checked />SHOW<br />
    </c:if>
    <c:if test="${flag==false }">
    ${node.title } <input type="checkbox" name="nav_id" value="${node.id }" />SHOW<br />
    </c:if>
    </c:if>
    <c:if test="${node.class_layer==2 }">
    <c:set var="flag" value="false"></c:set>
      &nbsp;&nbsp;&nbsp;&nbsp;|-${node.title }
      <c:forEach items="${rvList }" var="rvBean">
      <c:if test="${node.id==rvBean.nav_id }">
   <c:set var="flag" value="true"></c:set>
   </c:if>
       </c:forEach>
       
       <c:if test="${flag==true }">
       <input type="checkbox" name="nav_id" value="${node.id }" checked />SHOW <br />
       </c:if>
       
       <c:if test="${flag==false }">
       <input type="checkbox" name="nav_id" value="${node.id }" />SHOW <br />
       </c:if>
       
    </c:if>
    </c:forEach>
    </div>
    
    
    </form>
    </c:if>
  </body>
</html>
